package com.example.administrator.nurse.present;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.nurse.R;
import com.example.administrator.nurse.base.BaseActivity;
import com.example.administrator.nurse.common;
import com.example.administrator.nurse.model.bean.PatientRoomBean;
import com.example.administrator.nurse.widget.LoginInputEdit;
import com.loopj.android.http.RequestParams;

import java.text.SimpleDateFormat;
import java.util.Date;

import jjobes.SlideDateTimeListener;
import jjobes.SlideDateTimePicker;

/**
 * Created by Administrator on 2016/5/3.
 */
public class PatientInfoActivity extends BaseActivity {

    TextView state;


    @Override
    protected int setLayotId() {
        return R.layout.patient_info_activity;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
    }

    private int onSelectStatus = -1;
    private void initView(){
        configCommonHeader(PatientInfoActivity.this, "病房巡视详情", 1);

        /** 获取跳转过来的数据 */
        Intent intent = getIntent();
        final PatientRoomBean bean = (PatientRoomBean) intent.getSerializableExtra("data");

        //final PatientRoomBean bean=new PatientRoomBean("8","9","2016-3-23","12:11:23","2","王小二","女","68岁",0);
        TextView textView=new TextView(PatientInfoActivity.this);
        textView.setText("巡视日志");
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(PatientInfoActivity.this, PatientLogActivity.class);
                intent.putExtra("name", bean.getName());
                intent.putExtra("idOfPatient", bean.getId());
                startActivity(intent);
            }
        });
        RelativeLayout header= (RelativeLayout) findViewById(R.id.commonheader);
        RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.TRUE);
        params.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
        params.rightMargin=(int)toDip(PatientInfoActivity.this,10);
        textView.setTextColor(getResources().getColor(R.color.nav_color));
        textView.setLayoutParams(params);
        header.addView(textView);

        /** ---------------------数据绑定-------------------- */
        View dataView = f(R.id.dataView);
        TextView name   = (TextView) dataView.findViewById(R.id.name);
        TextView sex    = (TextView) dataView.findViewById(R.id.sex);
        TextView age    = (TextView) dataView.findViewById(R.id.age);
        TextView room   = (TextView) dataView.findViewById(R.id.room);
        TextView doctor = (TextView) dataView.findViewById(R.id.doctor);
        final TextView time   = (TextView) findViewById(R.id.time);
        TextView room_num = (TextView) dataView.findViewById(R.id.room_num);
        TextView bed_num  = (TextView) dataView.findViewById(R.id.bed_num);

        name.setText("姓名:"+bean.getName());
        sex.setText("性别:"+bean.getSex());
        age.setText("年龄:"+bean.getAge());
        room.setText("科室:"+bean.getDMent());
        doctor.setText("医师:"+bean.getDoc());
        time.setText(bean.getTime());
        room_num.setText(bean.getRoomnum());
        bed_num.setText(bean.getBednum());

        state= (TextView) findViewById(R.id.state);

        /**
         case -1:l.add("全部");break;
         case 0:l.add("在外出");break;
         case 1:l.add("在病房");break;
         case 2:l.add("家属陪伴外出");break;
         case 3:l.add("外出检查");break;
         case 4:l.add("未记录");break;
         * */
        Log.d("zzzzz","bean.getState() "+bean.getState());
        onSelectStatus = bean.getState();
        switch(bean.getState()){
            case -1:
                onSelectStatus = -1;
                state.setText("全部");
                break;
            case 0:
                onSelectStatus = 0;
                state.setText("在外出");
                break;
            case 1:
                onSelectStatus = 1;
                state.setText("在病房");
                break;
            case 2:
                onSelectStatus = 2;
                state.setText("家属陪伴外出");
                break;
            case 3:
                onSelectStatus = 3;
                state.setText("外出检查");
                break;
            case 4:
                onSelectStatus = 4;
                state.setText("未记录");
                break;
        }
        int[] flagList={R.mipmap.red_flag, R.mipmap.orange_flag, R.mipmap.yellow_flag, R.mipmap.green_flag};
        ImageView level_flag = (ImageView) dataView.findViewById(R.id.level_flag);
        level_flag.setImageResource(flagList[bean.getLevel()]);

        final LoginInputEdit watchRecord = (LoginInputEdit) f(R.id.watchRecord);
        watchRecord.setTextStr(bean.getContent());

        /** ----------------------------数据绑定完毕-------------------------- */

        final ImageView statusImage = (ImageView) f(R.id.select_state);
        statusImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PopupMenu popupMenu2 = new PopupMenu(PatientInfoActivity.this, statusImage);
                popupMenu2.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        /**
                         <item android:id="@+id/out"
                         android:title="在外出"/>
                         <item android:id="@id/room"
                         android:title="在病房"/>
                         <item android:id="@+id/out_family"
                         android:title="家属陪伴外出"/>
                         <item android:id="@+id/outcheck"
                         android:title="外出检查"/>
                         <item android:id="@+id/not_recode"
                         android:title="未记录"/>
                         <item android:id="@+id/all"
                         android:title="全部"/>
                         * */
                        switch (item.getItemId()){
                            case R.id.all:      state.setText("全部");break;
                            case R.id.out:      state.setText("在外出");break;
                            case R.id.room:     state.setText("在病房");break;
                            case R.id.out_family:state.setText("家属陪伴外出");break;
                            case R.id.outcheck:  state.setText("外出检查");break;
                            case R.id.not_recode:state.setText("未记录");break;
                        }
                        return true; // 返回 true 点击关闭
                    }
                });
                getMenuInflater().inflate(R.menu.patient_state, popupMenu2.getMenu());
                popupMenu2.show();
            }
        });
//        RelativeLayout patient_state= (RelativeLayout) findViewById(R.id.patient_state);


        findViewById(R.id.time).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(final View v) {
                new SlideDateTimePicker.Builder(getSupportFragmentManager())
                        .setListener(new SlideDateTimeListener() {
                            @Override
                            public void onDateTimeSet(Date date) {
                                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

                                String time = formatter.format(date);
                                /** 把日期存进去 */
                                ((TextView) v).setText(time);
                            }
                        })
                        .setInitialDate(new Date())
                        .setIs24HourTime(true)
                        .build()
                        .show();
            }
        });

        /** 保存按钮 */
        f(R.id.save).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(watchRecord.isOk("巡视记录不能为空")){
                    if(!time.getText().toString().equals("")){
                        startLoadingDialog();
                        RequestParams requestParams = new RequestParams();
                        requestParams.put("id",bean.getId());
                        requestParams.put("status",onSelectStatus);
                        requestParams.put("content",watchRecord.getStringText());
                        requestParams.put("time",time.getText().toString());
                        requestParams.put("idOfPatient",bean.getId());
                        requestParams.put("idOfNurse",common.nId);
                        requestParams.put("idOfHospital",common.nIdOfHospital);
                        doPost(0, null,common.Host + "/Controller/LghPatrolController.php?func=SaveRoom",requestParams);
                    }else{
                        Toast.makeText(PatientInfoActivity.this,"巡视时间不能为空",Toast.LENGTH_SHORT).show();
                    }
                }
            }
        });
    }


    @Override
    protected void onFailure(int requestCode, Object object, String errorMsg) {
        stopLoadingDialog();
    }

    @Override
    protected void onSuccess(int requestCode, Object object, String response) {
        stopLoadingDialog();
        if(response!=null){
            if(response.trim().equals("1")){
                Toast.makeText(PatientInfoActivity.this,"保存成功",Toast.LENGTH_SHORT).show();
                PatientInfoActivity.this.setResult(1);
                finish();
            }else{
                Toast.makeText(PatientInfoActivity.this,"保存失败,请稍后再试",Toast.LENGTH_SHORT).show();
            }
        }
    }


}
